package com.qidian.mes.cal.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.cal.mapper.CalPlanTeamMapper;
import com.qidian.mes.cal.service.CalPlanTeamService;
import com.qidian.mes.pojo.cal.CalPlanTeam;
import com.qidian.mes.pojo.cal.CalShift;
import com.qidian.mes.pojo.cal.CalTeam;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 04冯虹丹
 * @Company 起点编程
 * @Time 2023/11/2 16:01
 * @Description
 */
@Service
@Transactional
public class CalPlanTeamServiceImpl implements CalPlanTeamService {
    @Autowired
    private CalPlanTeamMapper calPlanTeamMapper;

    /**
     * 计划班组列表查询
     * @param calPlanTeam
     * @return
     */
    @Override
    public MESResult list(int page, int limit, CalPlanTeam calPlanTeam) {
        PageHelper.startPage(page,limit);
        List<CalPlanTeam> list = calPlanTeamMapper.list(calPlanTeam);
        PageInfo<CalPlanTeam> pageInfo = new PageInfo<>(list);
        return MESResult.ok(pageInfo);
    }

    /**
     * 添加计划班组
     * @param teamIds
     * @param planId
     * @return
     */
    @Override
    public MESResult save(Long[] teamIds, Long planId) {
        for (int i = 0; i < teamIds.length-1; i++) {
            if(calPlanTeamMapper.selectCalPlanTeam(teamIds[i],planId) != null){
                return MESResult.duplicate("班组已添加,不能重复添加!");
            }else{
                CalTeam calTeam = calPlanTeamMapper.selectCalTeam(teamIds[i]);
                CalPlanTeam calPlanTeam = new CalPlanTeam();
                calPlanTeam.setPlanId(planId);
                calPlanTeam.setTeamId(calTeam.getTeamId());
                calPlanTeam.setTeamCode(calTeam.getTeamCode());
                calPlanTeam.setTeamName(calTeam.getTeamName());
                calPlanTeam.setRemark(calTeam.getRemark());
                calPlanTeamMapper.save(calPlanTeam);
            }
        }
        return MESResult.ok();
    }


    /**
     * 单个删除计划班组
     * @param recordId
     * @return
     */
    @Override
    public MESResult delete(Long recordId) {
        return MESResult.updateIsOk(calPlanTeamMapper.delete(recordId));
    }

    /**
     * 根据计划班组Id获取班组成员
     * @param recordId
     * @return
     */
    @Override
    public MESResult getMember(Long recordId) {
        System.out.println("===================="+calPlanTeamMapper.getMember(recordId));
        return MESResult.ok(calPlanTeamMapper.getMember(recordId));
    }
}
